package com.acc.store.contorller;

import com.acc.store.common.Result;
import com.acc.store.entity.ShopCar;
import com.acc.store.service.ShopCarService;
import com.acc.store.vo.PieVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/car")
@Slf4j
@Tag(name = "购物车操作")
public class ShopCarContorller {
    @Autowired
    ShopCarService shopCarService;
    @Operation(summary = "获取饼图数据")
    @GetMapping("/pie")
    public Result pie(){
        Map<String, Integer> maps=shopCarService.pie();
        List<PieVo> vos = new ArrayList<>();
        for (Map.Entry<String, Integer> stringIntegerEntry : maps.entrySet()) {
            PieVo pieVo = new PieVo();
            pieVo.setValue(stringIntegerEntry.getValue());
            pieVo.setName(stringIntegerEntry.getKey());
            vos.add(pieVo);
        }
        return Result.success(vos);
    }

    @DeleteMapping("/delete/{carId}")
    @Operation(summary = "删除购物车")
    public Result delete(@PathVariable Long carId){
        shopCarService.removeById(carId);
        return Result.success(null);
    }

    @GetMapping("/list/{orderId}")
    @Operation(summary = "查询相应的购物车列表")
    public Result list(@PathVariable Long orderId){

        List<ShopCar> list=shopCarService.getList(orderId);
        return Result.success(list);
    }
    @PutMapping("/num")
    @Operation(summary = "更新购物车数量")
    public  Result num(@RequestParam Long id,@RequestParam Integer num){
        shopCarService.num(id,num);

        return Result.success("");

    }

    @PutMapping("/add")
    @Operation(summary = "新增购物车")
    public Result addr(@RequestParam Long orderId,@RequestParam Long productId){

        shopCarService.addr(orderId,productId);

        return Result.success("");


    }
}
